import {Component} from '@angular/core';
import {App, NavController} from 'ionic-angular';
//三级跳转
import {inexistDealerPage} from './inexistDealer/inexistDealer'
import {existDealerPage} from './existDealer/existDealer'
//  http请求
import {Http} from '@angular/http';
import {interfaceUrls} from '../../../provider/serverUrls';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/timeout';
import {UserService} from "../../../providers/user-service";
import {AppPopup} from "../../../util/popup";
import {UserLogin} from "../../../modules/app-user/user-login/user-login";
//通过手机号查询到的数据传递到已存在的经销商页面
@Component({
    selector: 'page-addTrench', 
    templateUrl: 'addTrench.html'
})
export class addTrenchPage {
    //需要在另外一个页面传给后台，组件间数据传递
    trenchName;
    trenchMobile;
    //http请求
    product: any;
    isShow = false;
    isShow2 = false;    //判断手机号是否跟姓名相一致

    errMsg: string;
    showMsg: boolean = false;

    // isTel = false;
    isNameT = false;
    param = {
        "name": this.trenchName,
        "mobile": this.trenchMobile
    };
    infoData = {};

    constructor(public navCtrl: NavController,
                public http: Http,
                private app: App,
                private userServ: UserService,
                private popup: AppPopup) {
    }

    ionViewWillEnter() {
        this.userServ.getPersonInfo()
            .then(() => {
            })
            .catch(err => {
                console.error("用户详情获取失败：", err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.app.getRootNav().setRoot(UserLogin);
                        });
                }
            });
    }

    addBack() {
		 this.navCtrl.pop();
	}
    addNext(){
        let tel = new RegExp('^1[3|4|5|7|8][0-9]{9}$');

        let name = new RegExp('^[(\u4E00-\u9FA5)| (\u0061-\u007a) | (\u0041-\u005a)]{2,8}$');
        if(!this.trenchName) {
            this.showMsg = true;
            this.errMsg = "请输入姓名";
        }else if(!name.test(this.trenchName)){
            this.showMsg = true;
            this.errMsg = "姓名字数为2-8个";
        }else if(!this.trenchMobile) {
            this.showMsg = true;
            this.errMsg = "请输入手机号";
        }else if(!tel.test(this.trenchMobile)) {
            this.showMsg = true;
            this.errMsg = "请输入正确手机号";
        } else if (name.test(this.trenchName) == true && tel.test(this.trenchMobile) == true) {

                console.log(this.trenchName, this.trenchMobile)
                this.http
                    .post(interfaceUrls.infoSearch, {
                        "name": this.trenchName, "mobile": this.trenchMobile
                    })
                    .timeout(2000)
                    .toPromise()
                    .then((response) => {
                        let resultStatus = response.json().resultStatus;
                        let data = response.json().infoData
                        if (resultStatus == "error") {
                            this.showMsg = true;
                            this.errMsg = response.json().errorMassage;
                        } else if (data !== null) {
                            //返回数据不为空说明就存在
                            this.infoData = response.json().infoData;
                            console.info(this.infoData);
                            this.navCtrl.push(existDealerPage, {
                                data: this.infoData,
                                product: this.product,
                                trenchName: this.trenchName,
                                trenchMobile: this.trenchMobile
                            })
                        } else {
                            this.infoData = response.json().infoData;
                            console.info(this.infoData);
                            this.navCtrl.push(inexistDealerPage, {
                                data: this.infoData, trenchName: this.trenchName, trenchMobile: this.trenchMobile
                            });
                        }

                    })
                    .catch(err => {
                        console.log('err');
                    })

        }
     
    }
}